<?php
/*
	页面类型
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
$return=array();
model('core/core')->checklogin();
$return['setting']=$this->plugin_setting('setting');
if($_RQ['op']=='main'){
	if($_RQ['code']){		
		$return['item']=pdo_get('jy_xunichong_code',array('uniacid'=>$_SESSION['uniacid'],'key'=>trim($_RQ['code'])));
		$return['item']['code']=trim($_RQ['code']);
		$return['card']=pdo_get('jy_xunichong_card',array('id'=>$return['item']['cardid']));
		$return['card']['typetitle']=$this->credit_type[$return['card']['type']];
	}else{
		$return['item']=array();
	}
	$return['pluginset']=$this->plugin_setting('setting');
	$return['isfollow']=pdo_getcolumn('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'follow');	
	if($return['pluginset']['isfollow'] && !$return['isfollow']){
		$return['needfollow']=true;
	}
	set_html('title','兑换中心');
	exi($return);
}

if($_RQ['op']=='post'){
	$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$set=$this->plugin_setting('setting');
	if($set['allmax']){
		$count=pdo_count('jy_xunichong_record',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'time >='=>$beginToday));
		if($set['allmax']<=$count){
				exi('您今日已达到最大兑换次数，请明天再来');
		}
	}
	$beginToday = mktime(0,0,0,date('m'),date('d'),date('Y'));
	$post=get_postdata();	
	$code=pdo_get('jy_xunichong_code',array('uniacid'=>$_SESSION['uniacid'],'key'=>trim($post['code'])));
	if(!$code){
		exi('兑换码错误','error');
	}
	if($code['status']==1){
		exi('兑换码已被使用','error');
	}
	if($code['endtime']<time()){
		exi('兑换码已过期','error');
	}
	$info=pdo_get('jy_xunichong_card',array('id'=>$code['cardid']));
	if($info['endtime']<time()){
		exi('兑换活动已结束','error');
	}
	$this->model('model')->sendcard($info,$code,$_SESSION['uid']);
	exi('兑换成功,已到账户','error');	
}